package reflect;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 39891
 * Date: 2025-04-14
 * Time: 21:05
 */
public class Test {
    public static void main(String[] args) {
        reflectNewInstance();
    }
    //写方法 通过反射获取对象
    public static void reflectNewInstance(){
        try {
            Class<?> c1 = Class.forName("reflect.Student");
            Student student = (Student) c1.newInstance(); //强转成student对象
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
























    public static void main1(String[] args) {

        //获取class对象的三种方式’ -> 获取的class对象是同一个
        Class<?> c2 = Student.class;

        Student student = new Student();
        Class<?> c3 = student.getClass();

        try {
            //()里面需要一个地址 最多使用
            Class<?> c1 = Class.forName("reflect.Student");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        System.out.println(c3 == c2);
    }
}
